package people.paper.tv.activity.home.modual.ydtj.presenter.impl;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.coocaa.app.support.net.Callback;
import com.coocaa.app.support.net.Result;

import java.util.ArrayList;
import java.util.List;

import people.paper.tv.activity.browser.BrowserActivity;
import people.paper.tv.activity.home.HomeFragment;
import people.paper.tv.activity.home.model.MainDataObject;
import people.paper.tv.activity.home.modual.ydtj.model.YDTJModel;
import people.paper.tv.activity.home.modual.ydtj.presenter.YDTJPresenter;
import people.paper.tv.activity.home.modual.ydtj.view.YDTJView;
import people.paper.tv.activity.home.modual.ywyl.view.data.NewsData;

public class YDTJPresenterImpl implements YDTJPresenter {
    private Context context;
    private YDTJView view;
    private YDTJModel model;

    @Override
    public void create(Context context, YDTJView view, YDTJModel model, HomeFragment homeFragment) {
        this.context = context;
        this.model = model;
        this.view = view;
    }

    @Override
    public void start() {
        showPageAt(-1);
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {
    }

    @Override
    public void onActivityStart() {

    }

    @Override
    public void onActivityResume() {

    }

    @Override
    public void onActivityPause() {

    }

    @Override
    public void onActivityStop() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void showPageAt(int pageIndex) {
        final int _pageIndex = pageIndex + 1;
        model.get(_pageIndex, new Callback<Result<List<MainDataObject.YWYLObject>>>() {
            @Override
            public Ret onResult(Result<List<MainDataObject.YWYLObject>> result) {
                if (_pageIndex == 0 && result.exception != null) {
                    view.showError(-1);
                } else {
                    List<NewsData> datas = new ArrayList<>();
                    for (final MainDataObject.YWYLObject obj : result.data) {
                        final NewsData data = new NewsData();
                        data.type = NewsData.TYPE_WEB;
                        data.title = obj.file_name;
                        data.imgUrl = obj.path;
                        data.onClick = new Runnable() {
                            @Override
                            public void run() {
                                if (TextUtils.isEmpty(obj.url))
                                    Toast.makeText(context, "人民日报后台未上传 " + data.title + " 关联Url数据", Toast.LENGTH_SHORT).show();
                                else
                                    BrowserActivity.start(context, obj.url);
                            }
                        };
                        datas.add(data);
                    }
                    view.onNewsLoaded(_pageIndex, datas);
                }
                return new Ret(0, "success~success~success~success");
            }
        });
    }
}
